﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System.Collections;


using LeMigue.Scrum.Iterface.Model;
using LeMigue.Scrum.TfsScrumV1.Service;


namespace LeMigue.Scrum.TfsScrumV1.Utils
{

    internal delegate T CreateScrumItem<T>(WorkItem wi, T item) where T : ScrumItem;

    internal delegate WorkItem CreateWorkItem<T>(T item, WorkItem wi) where T : ScrumItem;

    public static class Generics
    {
        internal static bool ContainParent(this WorkItemLinkCollection me, string linkType)
        {
            foreach(WorkItemLink link in me){
                if (link.LinkTypeEnd.Name == linkType)
                    return true;
            }
            return false;
        }

        
        internal static T ConvertToScrumItem<T>(this WorkItem me, CreateScrumItem<T> create) where T : ScrumItem
        {
            object[] parametros = new object[] { me.Id, me.Title };

            T bi = (T)Activator.CreateInstance(typeof(T), parametros);
            bi.Description = String.IsNullOrWhiteSpace(me.Description) ? "": me.Description;

            return create(me, bi);
        }

        internal static WorkItem ConvertToWorkItem<T>(this T me, WorkItem wi, CreateWorkItem<T> create) where T : ScrumItem
        {
            return create(me, wi);
        }

        internal static Node GetNode(this NodeCollection me, int idNode)
        {
            Node noReturn = null;
            foreach (Node no in me)
            {
                if (idNode == no.Id)
                {
                    noReturn = no;
                    break;
                }

                if (no.HasChildNodes){
                    noReturn = no.ChildNodes.GetNode(idNode);
                    if (noReturn != null)
                    {
                        break;
                    }
                }
            }

            return noReturn;
        }
    }
}
